package com.enjoy.book.action;
import com.alibaba.fastjson.JSON;
import com.enjoy.book.bean.Member;
import com.enjoy.book.bean.Record;
import com.enjoy.book.biz.MemberBiz;
import com.enjoy.book.biz.RecordBiz;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/member.let")
/**
 * @author 陈俊威
 */
public class MemberServlet extends HttpServlet{
    MemberBiz memberBiz=new MemberBiz();
    RecordBiz recordBiz =new RecordBiz();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MemberBiz memberBiz=new MemberBiz();
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out  = resp.getWriter();
        String type = req.getParameter("type");
        switch (type){
            case"add":
                add(req,resp,out);
                break;
            case"modify":
                modify(req,resp,out);
                break;
            case"modifypre":
                long bookId = Long.parseLong(req.getParameter("id"));
                Member member1 = memberBiz.getById(bookId);
                req.setAttribute("member1",member1);
                req.getRequestDispatcher("mem_modify.jsp").forward(req,resp);
                break;
            case"remove":
                long memId= Long.parseLong(req.getParameter("id"));
                try {
                    int count2 = memberBiz.remove(memId);
                    if(count2>0){
                        out.println("<script>alert('删除会员成功');location.href='member.let?type=query';</script>");
                    }else
                    {
                        out.println("<script>alert('删除会员失败');location.href='member.let?type=query';</script>");

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    out.println("<script>alert('删除会员失败');location.href='member.let?type=query';</script>");
                }
                break;
            case"query":
                List<Member> memberList=memberBiz.getAll();
                System.out.println(memberList.toString());
                req.setAttribute("memberList",memberList);
                req.getRequestDispatcher("mem_list.jsp").forward(req,resp);
                break;
            case "doajax":
                long id = Long.parseLong(req.getParameter("id"));
                Member member = memberBiz.getById(id);
                //member 转成json 字符串

                List<Record> records= recordBiz.getRecordByMemberId(id);
                if(records.size()>0)
                {
                   int amount1= member.getAmount();
                    int amount2 = records.size();
                    int size = amount1-amount2;
                    System.out.println(size);
                    member.setAmount(size);
                }
                String memberJson =JSON.toJSONString(member);
                //响应客户端
                out.print(memberJson);
                break;
            default:
        }
    }
    private void add(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        Member member =new Member();
        member.setName(req.getParameter("name"));
        member.setPwd(req.getParameter("pwd"));
        member.setIdNumber(req.getParameter("idNumber"));
        double balance= Double.parseDouble(req.getParameter("balance"));
        member.setBalance(balance);
        member.setTel(req.getParameter("tel"));
        int count = memberBiz.add(member);
        if(count>0){
            out.println("<script>alert('添加会员成功');location.href='member.let?type=query';</script>");
        }else{
            out.println("<script>alert('添加会员失败');location.href='member_add.jsp';</script>");
        }
    }
    private void modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        Member member =new Member();
        long id = Long.parseLong(req.getParameter("id"));
        member.setId(id);
        member.setName(req.getParameter("name"));
        member.setPwd(req.getParameter("pwd"));
        member.setIdNumber(req.getParameter("idNumber"));
        double balance= Double.parseDouble(req.getParameter("balance"));
        member.setBalance(balance);
        member.setTel(req.getParameter("tel"));
        memberBiz.modify(member);
        int count = memberBiz.modify(member);
        if(count>0){
            out.println("<script>alert('修改成功');location.href='member.let?type=query';</script>");
        }else{
            out.println("<script>alert('修改失败');location.href='member.let?type=query';</script>");
        }
    }
}
